//
//  PhoneViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/18.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "PhoneViewController.h"

@interface PhoneViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneNew;
@property (weak, nonatomic) IBOutlet UITextField *code;
@property(nonatomic,copy)NSString *codeStr;
@end

@implementation PhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if(!ISNOTNEEDMESSAGEBTN){
        self.isNeedMessageItem = YES;
    }

    self.phoneNew.text = USERPHONE;
}


- (IBAction)getCode:(id)sender {
    if(!_phoneNew.text.length) {
        [MBProgressHUD showTipMessageInWindow:@"请输入手机号码"];
        return;
    }
    if(![Tools isRightMobileNumber:_phoneNew.text]){
        [MBProgressHUD showTipMessageInWindow:@"手机号码格式不正确"];
        return;
    }
    
    //验证码
    NSDictionary *paramDic = @{@"phoneNum":self.phoneNew.text};
        __weak typeof(self)  weakSelf = self;
    [Tools AFNetworkingRequestTypePostWithUrl:GETCODE paramters:paramDic afterResultBlock:^(NSDictionary *resultDic) {
            if([resultDic[@"flag"] isEqualToString:@"true"]){
                [MBProgressHUD showTipMessageInWindow:@"验证码发送成功"];
                DLog(@"验证吗--%@",resultDic);
                NSDictionary *dataDic = resultDic[@"data"];
                weakSelf.codeStr =dataDic[@"CAPTCHA"];
            }else{
                if(resultDic[@"errorString"]){
                    [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
                }
            }
        }];
}
- (IBAction)bind:(id)sender {
    if(!_phoneNew.text.length) {
        [MBProgressHUD showTipMessageInWindow:@"请输入手机号码"];
        return;
    }
    if(![Tools isRightMobileNumber:_phoneNew.text]){
        [MBProgressHUD showTipMessageInWindow:@"手机号码格式不正确"];
        return;
    }
    if(!self.code.text.length){
        [MBProgressHUD showTipMessageInWindow:@"请输入验证码"];
        return;
    }
    if(![self.code.text isEqualToString:self.codeStr]){
        [MBProgressHUD showTipMessageInWindow:@"验证码错误"];
        return;
    }

    // 2.51更改绑定手机号
    NSDictionary *paraDic = @{
                              @"userId":USERID,
                              @"newPhone":self.phoneNew.text
                              };
    __weak typeof(self)  weakSelf = self;
    [Tools AFNetworkingRequestTypePostWithUrl:CHANGEPHONENUM paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"更改绑定手机号----%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSDictionary *dataDic = resultDic[@"data"];
            if([dataDic isKindOfClass:[NSNull class]]) return ;
            if([dataDic[@"status"] isEqualToString:@"true"]){
               [MBProgressHUD showTipMessageInWindow:@"更改绑定手机号码成功"];
               [weakSelf performSelector:@selector(popToSettingVc) withObject:nil afterDelay:1];
            }else{
                if(dataDic[@"errorString"]){
                    [MBProgressHUD showTipMessageInWindow:dataDic[@"errorString"]];
                }
            }
        }else{
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}

-(void)popToSettingVc{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
